<script type="text/javascript" src="../scripts/date.js"></script>
	<script type="text/javascript">
	function RutSoTietKiemXuLy()
	{
		document.getElementById("formgui").action = "index.php?do=rutsotietkiem_xuly";
		document.formgui.submit(); 
	}
	function checkRutLai(){
	 	var xmlHttp;
	 	var MaSoTietKiem = document.getElementById('MaSoTietKiem').value;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
		        var NgayLanhLai = document.getElementById('NgayLanhLai').value;
		        var NgayDenHan = document.getElementById('NgayDenHan2').value;
	        	if ((new Date(NgayDenHan).getTime() > new Date(NgayLanhLai).getTime())) {
	                alert("Chưa đến ngày lãnh lãi. Ngày lãnh lãi phải >= "+NgayDenHan);
	                document.getElementById("rutlai").checked = false;
	                var TienLai = xmlHttp.responseText;
	        		//document.getElementById('LaiPhaiTra').value = TienLai;
	        		//document.getElementById('SoTienNhan').value = TienLai;
	        	}
	        	else {
	        		var TienLai = xmlHttp.responseText;
	        		document.getElementById('LaiPhaiTra').value = TienLai;
	        		document.getElementById('SoTienNhan').value = TienLai;
	        	}
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/sotietkiem/checkrutlai.php?masotietkiem='+MaSoTietKiem, true);
	    xmlHttp.send(null);
	}
	function checkRutVonTruocHan(){
	 	var xmlHttp;
	 	var MaSoTietKiem = document.getElementById('MaSoTietKiem').value;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
		        var NgayLanhLai = document.getElementById('NgayLanhLai').value;
		        var NgayDenHan = document.getElementById('NgayDenHan2').value;
	        	if ((new Date(NgayDenHan).getTime() <= new Date(NgayLanhLai).getTime())) {
	                alert("Đã đến hạn rút lãi. Vui lòng chọn lại loại rút tiền");
	        	}
	        	else {
	        		var TienLai = xmlHttp.responseText;
	        		document.getElementById('LaiPhaiTra').value = TienLai;
	        		document.getElementById('SoTienNhan').value = Number(TienLai) + Number(document.getElementById('SoTienGui').value);
	        	}
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/sotietkiem/checkrutvontruochan.php?masotietkiem='+MaSoTietKiem, true);
	    xmlHttp.send(null);
	}
	function checkRutHoanToan(){
	 	var xmlHttp;
	 	var MaSoTietKiem = document.getElementById('MaSoTietKiem').value;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
		        var NgayLanhLai = document.getElementById('NgayLanhLai').value;
		        var NgayDenHan = document.getElementById('NgayDenHan2').value;
	        	if ((new Date(NgayDenHan).getTime() > new Date(NgayLanhLai).getTime())) {
	                alert("Chưa đến ngày để có thể rút hoàn toàn. Ngày rút phải >= "+NgayDenHan);
	              	document.getElementById("ruthoantoan").checked = false;
	                var TienLai = xmlHttp.responseText;
	        		//document.getElementById('LaiPhaiTra').value = TienLai;
	        		//document.getElementById('SoTienNhan').value = Number(TienLai) + Number(document.getElementById('SoTienGui').value);
	        	}
	        	else {
	        		var TienLai = xmlHttp.responseText;
	        		document.getElementById('LaiPhaiTra').value = TienLai;
	        		document.getElementById('SoTienNhan').value = Number(TienLai) + Number(document.getElementById('SoTienGui').value);
	        	}
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/sotietkiem/checkruthoantoan.php?masotietkiem='+MaSoTietKiem, true);
	    xmlHttp.send(null);
	}
	function tatToanSo(){
	 	var xmlHttp;
	 	var MaSoTietKiem = document.getElementById('MaSoTietKiem').value;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
		        var NgayLanhLai = document.getElementById('NgayLanhLai').value;
		        var NgayDenHan = document.getElementById('NgayDenHan2').value;
	        	if ((new Date(NgayDenHan).getTime() > new Date(NgayLanhLai).getTime())) {
	                alert("Chưa đến ngày để có thể tất toán sổ. Ngày tất toán phải >= "+NgayDenHan);
	              	document.getElementById("tattoanso").checked = false;
	                var TienLai = xmlHttp.responseText;
	        		//document.getElementById('LaiPhaiTra').value = TienLai;
	        		//document.getElementById('SoTienNhan').value = Number(TienLai) + Number(document.getElementById('SoTienGui').value);
	        	}
	        	else {
	        		var TienLai = xmlHttp.responseText;
	        		document.getElementById('LaiPhaiTra').value = TienLai;
	        		document.getElementById('SoTienNhan').value = Number(TienLai) + Number(document.getElementById('SoTienGui').value);
	        	}
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/sotietkiem/checktattoanso.php?masotietkiem='+MaSoTietKiem, true);
	    xmlHttp.send(null);
	}
</script>
<?php
	$sotietkiem = new sotietkiembs();
	$sotietkiem->MaSoTietKiem = $_GET["id"];
	$danhsach = $sotietkiem->LayDanhSach();
?>
<h2><strong>Rút tiền tiết kiệm</strong></h2>
<br/>
<form id="formgui" enctype="multipart/form-data" name="formgui"  method="post" action="index.php?do=inphieuchi&id=<?php echo $_GET["id"];?>" onsubmit="return checkPostArtForm(this);">
<table style="width:950px; margin-left: 10px; table-layout: fixed;">
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="masotietkiem">Chọn giao dịch:</label>&nbsp<span class="requirefield">*</span> 
	        </td>
	        <td style="width:15px;padding-top:10px;">
	            <input name="loairuttien" type="radio" id="rutlai" value="rutlai" onclick="checkRutLai();"/>Rút lãi 
	        </td>
	        <td style="width:15px;padding-top:10px;">
				<input name="loairuttien" type="radio" id="rutvontruochan" onclick="checkRutVonTruocHan();" value="rutvontruochan"/>Rút vốn trước hạn
	        </td>
	        <td style="width:15px;padding-top:10px;">
	            <input name="loairuttien" type="radio" id="ruthoantoan" onclick="checkRutHoanToan();" value="ruthoantoan"/>Rút hoàn toàn
	        </td>
	        <td style="width:15px;padding-top:10px;">
				<input name="loairuttien" type="radio" id="tattoanso" onclick="tatToanSo();" value="tattoanso"/>Tất toán sổ
	        </td>
		</tr>
		<p class="requirefields">(*): Các trường bắt buộc phải có.</p>
</table>
<br/>
<table style="width:950px; margin-left: 10px; table-layout: fixed;">	
		<tr>
			<td style="width:20px;padding-top:10px;">
	        	<label for="masotietkiem">Số sổ:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	           	<input readonly="readonly" tabindex="1" type="text" id="MaSoTietKiem" name="MaSoTietKiem" class="txt" size="54" value="<?php echo $danhsach[0]["MaSoTietKiem"];?>"/>   
	        </td>
	        <td style="width:15px;padding-top:10px;">

	        </td>
	        <td style="width:60px;padding-top:10px;">

	        </td>
		</tr>
		<tr>
			<?php 
				$kh = new khachhangbs();
				$kh->MaKH = $danhsach[0]["MaKH"];
				$dskhachhang = $kh->LayDanhSach();
			?>
			<td style="padding-top:10px;">
	        	<label for="tenkhachhang">Khách hàng:</label>
	        </td>	       
			<td style="padding-top:10px;">
				<input readonly="readonly" tabindex="1" type="text" id="TenKhachHang" name="TenKhachHang" class="txt" size="12" value="<?php echo $dskhachhang[0]['Ho'] . " ". $dskhachhang[0]['Ten'];?>" /> 
			</td>
			<td style="padding-top:10px;">
	        	<label for="makhachhang">Mã khách hàng:</label>      	
	        </td>
			 <td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="MaKH" name="MaKH" class="txt" size="12" value="<?php echo $dskhachhang[0]['MaKH'];?>" /> 
	        </td>
		</tr>		
		<tr>
			<td style="padding-top:10px;">
	        	<label for="loaitietkiem">Loại kỳ hạn:</label>
	        </td>
			<td style="padding-top:10px;">
				<select disabled="disabled" name="MaKyHan">	
            		<option value="0">-- Chọn kỳ hạn --</option>	                      	
					<?php
						$kyhan = new loaikyhanbs();
						$danhsachkyhan = $kyhan->LayDanhSach();
						for($j = 1; $j < ($danhsachkyhan != false ? count($danhsachkyhan) : 1); $j++)
						{
							if($danhsachkyhan[$j]["MaLoaiKyHan"]==$danhsach[0]["MaLoaiKyHan"])		
								echo "<option selected value='{$danhsachkyhan[$j]["MaLoaiKyHan"]}'>{$danhsachkyhan[$j]["TenLoaiKyHan"]}</option>";
							else 
								echo "<option value='{$danhsachkyhan[$j]["MaLoaiKyHan"]}'>{$danhsachkyhan[$j]["TenLoaiKyHan"]}</option>";
						}
					?>
				</select>			
			</td>
			<td style="padding-top:10px;">
				<label for="laisuat">Lãi suất:</label>
	        </td>
			<td style="padding-top:10px;">
				<?php
					$laisuatgui = new laisuatguibs();
					$laisuatgui->MaLaiSuatGui = $danhsach[0]["MaLaiSuatGui"];
					$dslaisuatgui = $laisuatgui->LayDanhSach();
				?>
				<input readonly="readonly" tabindex="1" type="text" id="LaiSuat" name="LaiSuat" class="input" size="12" value="<?php echo $dslaisuatgui[0]["PhanTramLaiSuatGui"];?>" />%	       
		        &nbsp
		        <select disabled="disabled" id ="HinhThucThangNam" name="HinhThucThangNam">	                      	
						<option <?php if($dslaisuatgui[0]["HangThang"]==1) echo "selected";?> value="Thang">Tháng</option>
						<option <?php if($dslaisuatgui[0]["HangNam"]==1) echo "selected";?> value="Nam">Năm</option>
				</select>
				<input type="hidden" id="HinhThucThangNam2" name="HinhThucThangNam2" value = "<?php if($dslaisuatgui[0]["HangThang"]==1) echo "Thang"; else echo "Nam";?>"/>
			 </td>
		</tr>
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="kyhan">Kỳ hạn:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	            <?php 
	        		$kyhan_2 = new loaikyhanbs();
	        		$kyhan_2->MaLoaiKyHan = $danhsach[0]["MaLoaiKyHan"];
	        		$danhsachkyhan_2 = $kyhan_2->LayDanhSach();
	        	?>
	        	<input type="hidden" id="MaKyHan2" name="MaKyHan2" value = "<?php echo $danhsach[0]["MaLoaiKyHan"];?>"/>
	            <input readonly="readonly" tabindex="1" type="text" id="KyHan" name="KyHan" class="txt" size="12" value="<?php echo $danhsachkyhan_2[0]["KyHan"];?>" />
	        </td>             
		</tr>
		<tr>	
	        <td style="padding-top:10px;">
	        	<label for="ngaymoso">Ngày lãnh lãi:</label>
	        </td>
	        <?php 
		        date_default_timezone_set('Asia/Ho_Chi_Minh');
	        ?>
			<td style="padding-top:10px;">
				<input readonly="readonly" tabindex="1" type="text" id="NgayLanhLai" name="NgayLanhLai" class="txt" size="12" value="<?php echo date("Y-m-d", time());?>" />
			</td>   
	    </tr>
		<tr>
			<td style="padding-top:10px;">
	        	<label for="ngaymoso">Ngày mở sổ:</label> 
	        </td>
			<td style="padding-top:10px;">
				<input readonly="readonly" tabindex="1" type="text" id="NgayMoSo" name="NgayMoSo" class="txt" size="12" value="<?php echo $danhsach[0]["NgayMoSo"];?>" />
			</td>
			<td style="padding-top:10px;">
				<label for="ngaydenhan">Ngày đến hạn:</label>
	        </td>
			<td style="padding-top:10px;">
				<input type="hidden" id="NgayDenHan2" value="<?php echo $danhsach[0]["NgayDenHan"];?>"/>
				<input readonly="readonly" tabindex="1" type="text" id="NgayDenHan" name="NgayDenHan" class="txt" size="12" value="<?php echo $danhsach[0]["NgayDenHan"];?>" />
	        </td>
		</tr>
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="sotiengui">Số tiền gửi:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	            <input readonly="readonly" tabindex="1" type="text" id="SoTienGui" name="SoTienGui" class="txt" value="<?php echo $danhsach[0]["SoDuKyHanTT"];?>" />
	        </td>
	        <td style="width:15px;padding-top:10px;">
				<label for="loaitiengui">Loại tiền gửi:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
				<input readonly="readonly" tabindex="1" type="text" id="LoaiTienGui" name="LoaiTienGui" class="txt" size="12" value="VND" />
	        </td>    
		</tr>
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="sotiengui">Lãi phải trả:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	            <input readonly="readonly" tabindex="1" type="text" id="LaiPhaiTra" name="LaiPhaiTra" class="txt" size="54" required oninvalid="this.setCustomValidity('Bạn chưa số tiền gửi')" onchange="this.setCustomValidity('')"/>   
	        </td>
	        <td style="width:15px;padding-top:10px;">
				<label for="loaitiengui">Số tiền nhận:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
				<input readonly="readonly" tabindex="1" type="text" id="SoTienNhan" name="SoTienNhan" class="txt" size="54" value="<?php if(isset($_GET['sotiennhan'])) echo $_GET['sotiennhan'];?>" required oninvalid="this.setCustomValidity('Bạn chưa số tiền gửi')" onchange="this.setCustomValidity('')"/>   
	        </td>    
		</tr>
		<tr>
			<td colspan="0">
			<hr>
			</td>
		</tr>
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="sobuttoan">Số bút toán:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	            <input readonly="readonly" tabindex="1" type="text" name="SoButToan" class="txt" size="54" value="<?php include 'getsobuttoan.php';?>"/>   
	        </td>
	        <td style="width:15px;padding-top:10px;">

	        </td>
	        <td style="width:60px;padding-top:10px;">

	        </td>
		</tr>
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="nhanvienlapso">Nhân viên thực hiện:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	        	<?php 
	        		$nguoidung = new nguoidungbs();
	        		$nguoidung->MaNguoiDung = $_SESSION["MaND"];
	        		$danhsach = $nguoidung->LayDanhSach();
	        		$HoTen = $danhsach[0]["Ho"]." ".$danhsach[0]["Ten"];
	        	?>
	        	<input type="hidden" name="MaNhanVien" value="<?php echo $danhsach[0]['MaNguoiDung'] ?>"/>
	            <input readonly="readonly" tabindex="1" type="text" name="TenNhanVien" class="txt" size="54" value="<?php echo $HoTen; ?>"/>   
	        </td>
	        <td style="width:15px;padding-top:10px;">

	        </td>
	        <td style="width:60px;padding-top:10px;">

	        </td>
		</tr>		
</table>

<br/>
<br/>
<p align="center">
	<a href="javascript:RutSoTietKiemXuLy();" class="button small blue">Hoàn tất</a>	
	&nbsp &nbsp &nbsp
	<input type="submit" class="button small blue" value="In phiếu lĩnh tiền"/>
	
	
</p>
</form>

<?php
	if(isset($_SESSION["LoiRutSoTietKiem"]))
	{
		echo $_SESSION["LoiRutSoTietKiem"];
		unset($_SESSION["LoiRutSoTietKiem"]);
	}
?>

<script type="text/javascript">
function checkPostArtForm(frm)
{
	var loairuttien = document.getElementsByName("loairuttien");
	<?php 
		if(isset($_GET["sotiennhan"]))
			echo "return true";
	?>
	if(!loairuttien.item(0).checked && !loairuttien.item(1).checked && !loairuttien.item(2).checked && !loairuttien.item(3).checked)
	{
		alert('Bạn chưa chọn loại rút tiền');
		return false;
	}
	return true;
}
</script>
